<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './professionTableData';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { professionSaveApi,professionUpdateApi } from '/@/api/website/websiteProfessionApi';
  import {getClassificationTreeApi} from "/@/api/website/websiteClassificationApi";

  export default defineComponent({
    name: 'ProfessionDrawer',
    components: { BasicDrawer, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);

      const professionId = ref('');

      const [registerForm, { resetFields, setFieldsValue , validate,updateSchema }] = useForm({
        labelWidth: 150,
        schemas: formSchema,

        showActionButtonGroup: false,
        baseColProps: { lg: 24, md: 24 },
      });

      const [registerDrawer, { setDrawerProps, closeDrawer, }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;
        professionId.value = data?.professionInfo?.professionId;

        if (unref(isUpdate)) {
          if(data.professionInfo.coverImage){
            data.professionInfo.coverImage = [ data.professionInfo.coverImage ];
          }
          setFieldsValue({
            ...data.professionInfo,
          });

        }
        const treeData = await getClassificationTreeApi();
        // 获取树的所有祖先节点名称
        const getTreeParents = ( data ) => {
          data.forEach((item) => {
            item.classificationId=item.id;
            console.log(1);
            console.log(item);
            if (item.children && item.children.length) {
              getTreeParents(item.children);
            }
          });
        };
        getTreeParents(treeData);
        console.log(treeData);
        updateSchema({
          field: 'classificationId',
          componentProps: { treeData },
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增专业' : '编辑专业'));

      async function handleSubmit() {
        try {
          const values = await validate();
          values.professionId = professionId.value;
          setDrawerProps({ confirmLoading: true });
          // TODO custom api
          values.coverImage = values.coverImage[0] ;
          console.log(values);
          await (!unref(isUpdate) ? professionSaveApi(values) : professionUpdateApi(values));
          closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      return { registerDrawer, registerForm, getTitle, handleSubmit };
    },
  });
</script>
